package com.kexun;

import java.text.Collator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Student implements Comparable<Student>{
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(int age, String name) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

        //获取中文排序对象collator,    int compare(Object o1, Object o2) 比较此方法两个参数的顺序。
    private Collator collator= Collator.getInstance(Locale.CHINA);
    @Override
    public int compareTo(Student o) {
        int i = this.getAge() - o.getAge();
        if(i == 0){

            return collator.compare(this.getName(),o.getName());   //中文拼音排序

        }
        return i;
    }


    /*注意如果compareTo方法 直接返回0,那么treeMap集合只能存一个对象   ,而且 value还将被后面的覆盖
      导致  小白 变成深圳人 所以最好用treeSet保存对象 把地址 也放到对象中*/
    public static void main(String[] args) {
        TreeMap<Student, String> treeMap=new TreeMap<>();
        Student s1=new Student(10,"小白");
        Student s2=new Student(11,"黑");
        Student s3=new Student(10,"阿黄");
        Student s4=new Student(13,"小亮");
        treeMap.put(s1, "北京");
        treeMap.put(s2, "上海");
        treeMap.put(s3, "广州");
        treeMap.put(s4, "深圳");
        System.out.println(treeMap);
        System.out.println("---entryset---");
        Set<Map.Entry<Student, String>> s=treeMap.entrySet();
        for (Map.Entry<Student, String> entry : s) {
            System.out.println(entry);
        }

    }
}
